package com.waidi.clothespad.adapter;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.ArrayList;
public class ViewPageAdapter extends PagerAdapter {

    private ArrayList<ImageView> list_view;
    public ViewPageAdapter(ArrayList<ImageView> list_view)
    {
        this.list_view = list_view;
    }

    @Override
    public int getCount()
    {
        return list_view.size();
    }
    // 3. 指定复用的判断逻辑, 固定写法
    @Override
    public boolean isViewFromObject(View view, Object object) {
//			System.out.println("isViewFromObject: "+(view == object));
        // 当划到新的条目, 又返回来, view是否可以被复用.
        // 返回判断规则
        return view == object;
    }

    @Override
    public int getItemPosition(Object object) {
        // TODO Auto-generated method stub
        return PagerAdapter.POSITION_NONE;
    }


    // 1. 返回要显示的条目内容, 创建条目
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        System.out.println("instantiateItem初始化: " + position);
        // container: 容器: ViewPager
        // position: 当前要显示条目的位置 0 -> 4




        container.addView(list_view.get(position % list_view.size()));
        return list_view.get(position % list_view.size());




    /*        int newPosition = 0;
            if(imageViewList.size()>0)
            {
                newPosition = position % imageViewList.size();
            }
            else
                return null;

            ImageView imageView = imageViewList.get(newPosition);


            // TODO Auto-generated method stub
            //判断待添加的视图是否已存在父控件，若存在则调用removeAllViews()去除之
            ViewGroup parent = (ViewGroup) imageView.getParent();
            //Log.i("ViewPaperAdapter", parent.toString());
            if (parent != null) {
                parent.removeAllViews();
            }
            // a. 把View对象添加到container中
            container.addView(imageView);
            // b. 把View对象返回给框架, 适配器
            return imageView; // 必须重写, 否则报异常*/
    }
    // 2. 销毁条目
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // object 要销毁的对象
        System.out.println("destroyItem销毁: " + position);
        container.removeView((View) object);
        //  container.removeView(list_view.get(position % list_view.size()));
    }


}
